package com.sdtv.mediunfusion.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class SharedPreUtils {

    private static final String PREFERENCE_FLAG = "com.qingk.mmsmobilebg";

    public static String getPreStringInfo(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE);// 文件模式只有在创建的时候才使用
        return preferences.getString(key, "");
    }

    public static int getPreIntInfo(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE);// 文件模式只有在创建的时候才使用
        return preferences.getInt(key, 0);
    }

    public static int getPreIntInfo(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE);// 文件模式只有在创建的时候才使用
        return preferences.getInt(key, value);
    }

    public static long getPreLongInfo(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE);// 文件模式只有在创建的时候才使用
        return preferences.getLong(key, 0);
    }

    public static boolean getPreBooleanInfo(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE);// 文件模式只有在创建的时候才使用
        return preferences.getBoolean(key, false);
    }

    public static void setStringToPre(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        Editor dEditor = sharedPreferences.edit();
        dEditor.putString(key, value);
        dEditor.commit();
    }

    public static void setIntToPre(Context context, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        Editor dEditor = sharedPreferences.edit();
        dEditor.putInt(key, value);
        dEditor.commit();
    }

    public static void setLongToPre(Context context, String key, long value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        Editor dEditor = sharedPreferences.edit();
        dEditor.putLong(key, value);
        dEditor.commit();
    }

    public static void setBooleanToPre(Context context, String key,
                                       boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        Editor dEditor = sharedPreferences.edit();
        dEditor.putBoolean(key, value);
        dEditor.commit();
    }

    public static void removePre(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        Editor dEditor = sharedPreferences.edit();
        dEditor.remove(key);
        dEditor.commit();
    }

    public static SharedPreferences getSharePreference(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_WORLD_READABLE);
        return sharedPreferences;
    }

    /**
     * 保存List
     *
     * @param tag      保存数据的key
     * @param datalist
     */
    public static <T> void saveDataList(Context mContext, String tag, List<T> datalist) {
        SharedPreferences preferences = mContext.getSharedPreferences(PREFERENCE_FLAG, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
        Editor editor = preferences.edit();
        if (null != datalist && !datalist.isEmpty()) {
            Gson gson = new Gson();
            //转换成json数据，再保存
            String strJson = gson.toJson(datalist);
//            PrintLog.printDebug("saveDataList", "saveDataList size --: " + datalist.size() + " \n strJson  :  " + strJson);
            editor.putString(tag, strJson);
            editor.commit();
        } else {
            editor.clear();
            editor.commit();
        }
    }

    /**
     * 获取List
     *
     * @param tag 获取数据的key
     * @return
     */
    public static <T> List<T> getDataList(Context mContext, String tag, Type type) {
        SharedPreferences preferences = mContext.getSharedPreferences(
                PREFERENCE_FLAG, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);// 文件模式只有在创建的时候才使用
        List<T> datalist = new ArrayList<>();
        String strJson = preferences.getString(tag, "");
//        PrintLog.printDebug("getDataList", "tag:  " + tag + " \n strJson: " + strJson);
        if (TextUtils.isEmpty(strJson)) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, type);
        return datalist;
    }


}
